package com.common.baselibrary.util;

import android.os.Environment;

import java.io.File;

/**
 * Copyright (C), 2020-2020, ACC
 * Description: 文件存储
 * Created by M=54G on 2020/6/5 9:30 AM.
 */
public class FileUtil {
    public static String rootDir = Environment.getExternalStoragePublicDirectory("score-acc-data").getAbsolutePath();
    private static String mPath = rootDir + "/musicXml/";
    private static String rPath = rootDir + "/record/";
    private static String iPath = rootDir + "/image/";
    private static String lPath = rootDir + "/log/";

    public static String getLogPath() {
        makeSureDir(lPath);
        return lPath;
    }

    public static String getXmlPath(String fileName) {
        makeSureDir(mPath);
        return mPath + fileName;
    }

    public static String getRecordPath(String fileName) {
        makeSureDir(rPath);
        return rPath + fileName;
    }

    public static String getImgPath(String fileName) {
        makeSureDir(iPath);
        return iPath + fileName;
    }

    /**
     * 确保文件夹已经建立
     */
    private static void makeSureDir(String dir) {
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    /**
     * 清除XML缓存
     */
    public static void clearXmlCache() {
        deleteFiles(new File(mPath));
    }

    /**
     * 删除某个目录下的所有文件
     *
     * @param dir
     */
    public static void deleteFiles(File dir) {
        if (dir == null || !dir.exists() || !dir.isDirectory())
            return;
        for (File file : dir.listFiles()) {
            if (file.isFile())
                file.delete(); // 删除所有文件
        }
    }

    //public static String copyFile(Context context, int id, String filePath) {
    //    InputStream inputStream = context.getResources().openRawResource(id);
    //    File file = new File(filePath);
    //    File parent = file.getParentFile();
    //    if (parent != null && !parent.exists()) {
    //        parent.mkdirs();
    //    }
    //    return readInputStream(filePath, inputStream);
    //}

    ///**
    // * 读取输入流中的数据写入输出流
    // *
    // * @param storagePath 目标文件路径
    // * @param inputStream 输入流
    // */
    //private static String readInputStream(String storagePath, InputStream inputStream) {
    //    File file = new File(storagePath);
    //    try {
    //        if (!file.exists()) {
    //            // 1.建立通道对象
    //            FileOutputStream fos = new FileOutputStream(file);
    //            // 2.定义存储空间
    //            byte[] buffer = new byte[inputStream.available()];
    //            // 3.开始读文件
    //            int lenght = 0;
    //            while ((lenght = inputStream.read(buffer)) != -1) {  // 循环从输入流读取buffer字节
    //                // 将Buffer中的数据写到outputStream对象中
    //                fos.write(buffer, 0, lenght);
    //            }
    //            fos.flush();// 刷新缓冲区
    //            // 4.关闭流
    //            fos.close();
    //            inputStream.close();
    //        }
    //    } catch (Exception e) {
    //        e.printStackTrace();
    //    }
    //
    //    return file.getPath();
    //}
}
